package com.wudl.flink.core;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import javax.print.DocFlavor;

/**
 * TODO
 *
 * @author wudl
 * @version 1.0
 * @date 2021/3/5 17:03
 */
public class WorkCountYarn {
    public static void main(String[] args) throws Exception {
        ParameterTool parameterTool = ParameterTool.fromArgs(args);
        String output = "";
        if (parameterTool.has("output")) {
            output = parameterTool.get("output");
            System.out.println("指定了输出路径使用:" + output);
        } else {
            output = "hdfs://master01.pxx.com:8020/wordcount/outputwudl_";
            System.out.println("可以指定输出路径使用 --output ,没有指定使用默认的:" + output);
        }

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStream<String> lines = env.fromElements("flink", "base", "flink", "spark", "hive");
        SingleOutputStreamOperator<String> world = lines.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String value, Collector<String> out) throws Exception {
                String[] word = value.split(",");
                for (String w : word) {
                    out.collect(w);
                }
            }
        });

        SingleOutputStreamOperator<Tuple2<String, Integer>> wordmap = world.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String value) throws Exception {
                return Tuple2.of(value, 1);
            }
        });

        KeyedStream<Tuple2<String, Integer>, String> groupwork = wordmap.keyBy(t -> t.f0);
        groupwork.print();
        groupwork.sum(1);
        env.execute();



    }
}
